package com.vhall.component.controller.exam;

import com.vhall.component.controller.exam.facade.ExamConsoleFacade;
import com.vhall.component.entity.exam.dto.ExamAnswersDTO;
import com.vhall.component.entity.exam.dto.ExamDTO;
import com.vhall.component.entity.exam.dto.ExamInfoDTO;
import com.vhall.component.entity.exam.vo.*;
import com.vhall.component.entity.page.vo.PageVO;
import com.vhall.component.framework.common.annotation.VhallComponent;
import com.vhall.component.framework.common.exception.BusinessException;
import com.vhall.component.plugin.common.annotation.TokenAuth;
import com.vhall.component.plugin.common.exception.BizErrorCode;
import com.vhall.component.plugin.common.support.Token;
import com.vhall.component.plugin.common.support.TokenThreadLocal;
import com.vhall.component.service.exam.ExamConsoleService;
import com.vhall.component.service.exam.ExamService;
import com.vhall.component.service.exam.constant.ExamConst;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.HashMap;

/**
 * @author yongzheng.xu
 * @since 2021/7/5
 */
@TokenAuth
@VhallComponent
public class ExamConsoleController implements ExamConsoleFacade {

    @Autowired
    private ExamService examService;

    @Autowired
    private ExamConsoleService examConsoleService;

    @Override
    public ExamDTO paperCreate(ExamPaperCreateReqVo vo) {
        if (vo.getType().equals(ExamConst.TYPE_EXAM) && vo.getLimitTime() <= 0) {
            throw new BusinessException(BizErrorCode.COMP_EXAM_LIMIT_TIME_NOT_SET);
        }
        Token token = TokenThreadLocal.getToken();
        ExamDTO result = new ExamDTO();
        return examService.paperCreate(token.getAccountId(), vo);
    }

    @Override
    public ExamDTO paperInfo(ExamPaperCopyReqVo vo) {
        Integer examId = vo.getExamId();
        if (examId <= 0) {
            throw new BusinessException(BizErrorCode.COMP_EXAM_NOT_EXIST);
        }
        return examService.paperInfo(TokenThreadLocal.getToken().getAccountId(), examId);
    }

    @Override
    public PageVO<ExamDTO> paperList(ExamPaperListReqVo vo) {
        if (vo.getPageSize() > 1000) {
            vo.setPage(10);
        }
        if (null != vo.getKeyword() && vo.getKeyword().length() > 100) {
            vo.setKeyword(StringUtils.substring(vo.getKeyword(), 0, 20));
        }
        return examService.paperList(TokenThreadLocal.getToken().getAccountId(), vo);
    }

    @Override
    public boolean paperBind(ExamVo vo) {
        return examService.paperBind(TokenThreadLocal.getToken(), vo);
    }

    @Override
    public ExamDTO paperCopy(ExamPaperCopyReqVo vo) {
        Token token = TokenThreadLocal.getToken();
        ExamDTO result = new ExamDTO();
        return examService.paperCopy(token.getAccountId(), vo);
    }

    @Override
    public ExamInfoDTO info(ExamInfoVo vo) {
        return examService.info(TokenThreadLocal.getToken().getAccountId(), vo);
    }

    /**
     * 创建考试记录
     *
     * @param vo
     * @return
     */
    @Override
    public ExamDTO create(ExamCreateReqVo vo) {
        Token token = TokenThreadLocal.getToken();
        return examService.create(token.getAccountId(), vo);
    }

    @Override
    public ExamDTO update(ExamUpdateReqVo vo) {
        Token token = TokenThreadLocal.getToken();
        return examService.update(token.getAccountId(), vo);
    }

    /**
     * 试卷列表
     *
     * @param vo
     * @return
     */
    @Override
    public PageVO<ExamConsoleListRspVO> list(ExamConsoleListReqVo vo) {
        Token token = TokenThreadLocal.getToken();
        vo.setAccountId(token.getAccountId());
        if (StringUtils.isNotBlank(vo.getRoomId())) {
            vo.setAccountId(null);
        }
        return examConsoleService.list(vo);
    }

    /**
     * 批阅列表
     *
     * @param vo
     * @return
     */
    @Override
    public PageVO<ExamAnswersDTO> gradeList(ExamGradeListReqVo vo) {
        ExamAnswerListVo evo = new ExamAnswerListVo();
        BeanUtils.copyProperties(vo, evo);
        return examService.answeredList(evo);
    }

    /**
     * 试卷-删除记录
     *
     * @param vo
     * @return
     */
    @Override
    public Integer delete(ExamDeleteConsoleReqVo vo) {
        Token token = TokenThreadLocal.getToken();
        return examConsoleService.delete(token.getAccountId(), vo);
    }

    /**
     * 批阅
     *
     * @param vo
     * @return
     */
    @Override
    public Boolean graded(ExamGradeReqVo vo) {
        Token token = TokenThreadLocal.getToken();
        vo.setOperatorAccountId(token.getAccountId());
        vo.setOperatorNickname(token.getNickname());
        return examService.graded(vo);
    }

    /**
     * 批阅信息
     *
     * @param vo
     * @return
     */
    @Override
    public GradeInfoRspVO gradeInfo(ExamGradeInfoReqVo vo) {
        Token token = TokenThreadLocal.getToken();
        vo.setAccountId(token.getAccountId());
        return examService.gradedInfo(vo);
    }

    /**
     * 考试概况
     *
     * @param vo
     * @return
     */
    @Override
    public HashMap<String, Object> stat(ExamVo vo) {
        return examService.stat(vo);
    }

    /**
     * 回答列表
     *
     * @param vo
     * @return
     */
    @Override
    public PageVO<ExamAnswersDTO> answeredList(ExamAnswerListVo vo) {
        return examService.answeredList(vo);
    }

    /**
     * 回答导出
     *
     * @param vo
     * @return
     */
    @Override
    public Boolean exportAnswer(ExamAnswerExportReqVo vo) {
        Token token = TokenThreadLocal.getToken();
        vo.setAccountId(token.getAccountId());
        return examService.exportAnswer(vo);
    }

}
